package com.qinyunkj.searchchatserver.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qinyunkj.searchchatserver.model.common.Result;
import com.qinyunkj.searchchatserver.model.entity.CrawlerConfig;
import com.qinyunkj.searchchatserver.model.entity.CrawlerLog;
import com.qinyunkj.searchchatserver.service.CrawlerConfigService;
import com.qinyunkj.searchchatserver.service.CrawlerLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "爬虫管理")
@RestController
@RequestMapping("/api/crawler")
@RequiredArgsConstructor
public class CrawlerController {

    private final CrawlerConfigService crawlerConfigService;
    private final CrawlerLogService crawlerLogService;

    @Operation(summary = "获取爬虫配置")
    @GetMapping("/config")
    public Result<CrawlerConfig> getConfig() {
        return Result.success(crawlerConfigService.getEnabledConfig());
    }

    @Operation(summary = "更新爬虫配置")
    @PutMapping("/config")
    public Result<Boolean> updateConfig(@RequestBody CrawlerConfig config) {
        return Result.success(crawlerConfigService.updateById(config));
    }

    @Operation(summary = "获取爬虫日志")
    @GetMapping("/logs")
    public Result<Page<CrawlerLog>> getLogs(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(crawlerLogService.pageLog(pageNum, pageSize));
    }
} 